package com.acedemo.game2048.app;


import com.acedemo.game2048.MyApplication;
import com.acedemo.game2048.entity.CellEntity;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.List;

/**
 * 保存配置工具
 * <p>
 * Created by KYLE on 2018/10/4
 */
public class ConfigManager {


    private static Preferences getInstance(Context context, String name) {
        Preferences preferences = new DatabaseHelper(context).getPreferences(name);
        return preferences;
    }

    /**
     * 保存最高分
     */
    public static void putBestScore(Context context, String key, int bestScore) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_BEST_SCORE);
        //MyApplication.KEY_BEST_SCORE_WITHIN_4
        //MyApplication.KEY_BEST_SCORE_WITHIN_5
        //MyApplication.KEY_BEST_SCORE_WITHIN_6
        preferences.putInt(key, bestScore).flush();
    }

    /**
     * 获取最高分
     */
    public static int getBestScore(Context context, String key) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_BEST_SCORE);
        //MyApplication.KEY_BEST_SCORE_WITHIN_4
        //MyApplication.KEY_BEST_SCORE_WITHIN_5
        //MyApplication.KEY_BEST_SCORE_WITHIN_6
        return preferences.getInt(key, 0);
    }

    /**
     * 保存当前得分
     */
    public static void putCurrentScore(Context context, String key, int currentScore) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_SCORE);
        // 难度4 KEY_CURRENT_SCORE_4
        // 难度5 KEY_CURRENT_SCORE_5
        // 难度6 KEY_CURRENT_SCORE_6
        preferences.putInt(key, currentScore).flush();
    }

    /**
     * 获取当前得分
     */
    public static int getCurrentScore(Context context, String key) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_SCORE);
        // 难度4 KEY_CURRENT_SCORE_4
        // 难度5 KEY_CURRENT_SCORE_5
        // 难度6 KEY_CURRENT_SCORE_6
        return preferences.getInt(key, 0);
    }

    /**
     * 保存无限模式下最高分
     */
    public static void putBestScoreWithinInfinite(Context context, int bestScore) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_BEST_SCORE);
        preferences.putInt(MyApplication.KEY_BEST_SCORE_WITHIN_INFINITE, bestScore).flush();
    }

    /**
     * 获取无限模式下最高分
     */
    public static int getBestScoreWithinInfinite(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_BEST_SCORE);
        return preferences.getInt(MyApplication.KEY_BEST_SCORE_WITHIN_INFINITE, 0);
    }

    /**
     * 保存当前无限模式分数
     */
    public static void putCurrentScoreWithinInfinite(Context context, int currentScore) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_SCORE_INFINITE);
        preferences.putInt("KEY_CURRENT_SCORE_INFINITE", currentScore).flush();
    }

    /**
     * 获取当前无限模式分数
     */
    public static int getCurrentScoreWithinInfinite(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_SCORE_INFINITE);
        return preferences.getInt("KEY_CURRENT_SCORE_INFINITE", 0);
    }

    /**
     * 保存游戏难度
     */
    public static void putGameDifficulty(Context context, int difficulty) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GAME_DIFFICULTY);
        preferences.putInt(MyApplication.KEY_GAME_DIFFICULTY, difficulty).flush();
    }

    /**
     * 获取游戏难度
     */
    public static int getGameDifficulty(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GAME_DIFFICULTY);
        return preferences.getInt(MyApplication.KEY_GAME_DIFFICULTY, 4);
    }

    /**
     * 保存游戏音效状态
     */
    public static void putGameVolume(Context context, boolean volumeState) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GAME_VOLUME_STATE);
        preferences.putBoolean(MyApplication.KEY_GAME_VOLUME_STATE, volumeState).flush();
    }

    /**
     * 获取游戏音效状态
     */
    public static boolean getGameVolumeState(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GAME_VOLUME_STATE);
        return preferences.getBoolean(MyApplication.KEY_GAME_VOLUME_STATE, true);
    }

    /**
     * 保存达成游戏目标次数
     */
    public static void putGoalTime(Context context, int time) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GET_GOAL_TIME);
        preferences.putInt(MyApplication.KEY_GET_GOAL_TIME, time).flush();
    }

    /**
     * 获取达成游戏目标次数
     */
    public static int getGoalTime(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_GET_GOAL_TIME);
        return preferences.getInt(MyApplication.KEY_GET_GOAL_TIME, 0);
    }

    /**
     * 保存游戏模式
     */
    public static void putCurrentGameMode(Context context, int mode) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_GAME_MODE);
        preferences.putInt(MyApplication.KEY_CURRENT_GAME_MOE, mode).flush();
    }

    /**
     * 获取游戏模式
     */
    public static int getCurrentGameMode(Context context) {
        Preferences preferences = getInstance(context, MyApplication.SAVE_CURRENT_GAME_MODE);
        return preferences.getInt(MyApplication.KEY_CURRENT_GAME_MOE, 0);
    }

    /**
     * 保存游戏数据
     */
    public static void putTableData(Context context, String fileName, String key, String cellListStr) {
        Preferences preferences = getInstance(context, fileName);
        preferences.putString(key, cellListStr).flush();
    }

    /**
     * 保存游戏数据
     */
    public static String getTableData(Context context, String fileName, String key) {
        Preferences preferences = getInstance(context, fileName);
        return preferences.getString(key, "");
    }

}
